package com.dongdongshop.util;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientGetDemo {


    /*
     * 带参数的get请求
     * */
    public static String httpGetByData(String param,String value,String url) throws Exception {
        //1.创建一个HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建URIBuilder
        URIBuilder uriBuilder = new URIBuilder(url);
        //设置参数
        /*
         * 第一个参数是参数名称
         * 第二个参数是参数的值
         *
         * */
        uriBuilder.setParameter(param,value);

        //2.创建一个Get请求,设置url访问地址
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        CloseableHttpResponse response = null;
        String content = null;
        try {
            //3.使用HttpClient来执行Get请求,获取response
            response = httpClient.execute(httpGet);
            //4.处理response,解析相应  200:响应成功
            if(response.getStatusLine().getStatusCode() == 200){
                content = EntityUtils.toString(response.getEntity(), "utf8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //5.释放资源
                if(response != null){
                    response.close();
                }if(httpClient != null){
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }







//   /*
//     * 带参数的get请求调用天气预报接口
//     * */
//    @RequestMapping("httpGetWeatherForecastByGetParams")
//    @ResponseBody
//    public String httpGetByData(String city) throws URISyntaxException {
//        //1.创建一个HttpClient对象
//        CloseableHttpClient httpClient = HttpClients.createDefault();
//        //创建URIBuilder http://wthrcdn.etouch.cn/weather_mini?city=北京
//        URIBuilder uriBuilder = new URIBuilder("http://wthrcdn.etouch.cn/weather_mini");
//        //设置参数
//        /*
//         * 第一个参数是参数名称
//         * 第二个参数是参数的值
//         * */
//        uriBuilder.setParameter("city",city);
//        //2.创建一个Get请求,设置url访问地址
//        HttpGet httpGet = new HttpGet(uriBuilder.build());
//        CloseableHttpResponse response = null;
//        String content = null;
//        try {
//            //3.使用HttpClient来执行Get请求,获取response
//            response = httpClient.execute(httpGet);
//            //4.处理response,解析相应  200:响应成功
//            if(response.getStatusLine().getStatusCode() == 200){
//                /*
//                 * 第一个参数是响应体
//                 * 第二个参数是编码格式
//                 * */
//                content = EntityUtils.toString(response.getEntity(), "utf8");
//                System.out.println("响应状态为:"+response.getStatusLine());
//                System.out.println("响应内容为:"+ content);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                //5.释放资源
//                if(response != null){
//                    response.close();
//                }if(httpClient != null){
//                    httpClient.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return content;
//    }

}
